package com.du.web.sample.cofig;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.Executor;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * Desc:
 * Author: dushuang
 * Date: 2023/7/5
 **/
@Configuration
public class ThreadPoolConfig {

    @Bean("channelAsyncExecutor")
    public Executor channelAsyncExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        // 核心线程
        executor.setCorePoolSize(16);
        // 最大线程
        executor.setMaxPoolSize(32);
        // 阻塞队列大小
        executor.setQueueCapacity(1024);
        // 线程空闲等待时间
        executor.setKeepAliveSeconds(60);
        // 线程名称前缀
        executor.setThreadNamePrefix("ChannelAsyncPool-");
        // 拒绝策略(用主线程处理该任务-会影响性能，保证任务不丢失)
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        executor.setAllowCoreThreadTimeOut(true);
        executor.initialize();
        return executor;
    }

}
